<template>
    <a-card :bordered="false"
            class="card-area"
            style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
      <a-form :form="form">
        <a-form-item label='名称' v-bind="formItemLayout">
          <a-input v-decorator="['name',
                   {rules: [
                    { required: true, message: '名称不能为空'},
                    // {pattern:'^[\u4e00-\u9fa5\\\\w]+$',message:'请输入中文'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]" style="width: 50%"/>
        </a-form-item>
        <a-form-item label='编号' v-bind="formItemLayout">
          <a-input v-decorator="['code',
                   {rules: [
                    {pattern:'^[A-Za-z0-9]+$',message:'请输入数字和字母'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]" style="width: 50%"/>
        </a-form-item>
        <a-form-item label='排序' v-bind="formItemLayout">
          <a-input-number :min="1" v-decorator="['sort']" style="width: 50%"/>
        </a-form-item>
        <a-form-item label='类型' v-bind="formItemLayout">
          <a-tree
            :parentId="parentId"
            :key="cateTreeKeys"
            :checkable="true"
            :checkStrictly="true"
            @check="handleCheck"
            @expand="handleExpand"
            :expandedKeys="expandedKeys"
            :treeData="cateTreeData">
          </a-tree>
        </a-form-item>
      </a-form>
      <div class="drawer-bootom-button">
        <div style="width: 50%">
          <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
          <a-button @click="reset" type="primary" :loading="loading">重置</a-button>
        </div>
      </div>
    </a-card>
</template>
<script>
import db from 'utils/localstorage'
const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18}
}
export default {
  components: {},
  name: 'CategoryAddForm',
  data () {
    return {
      parentId: '',
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      checkedKeys: [],
      expandedKeys: [],
      // 主图
      defaultFileListM: [],
      // 附图
      defaultFileListA: [],
      headers: {
        Authentication: db.get('USER_TOKEN')
      },
      cateTreeData: [],
      cateTreeKeys: +new Date()
    }
  },
  created () {
    this.loading = true
    this.$api.goods.getCategoryList().then((r) => {
      this.loading = false
      console.log(r)
      this.cateTreeData = r.data.data.tree.children
    })
  },
  watch: {
    checkedKeys (checkedKeys) {
      console.log(checkedKeys)
    }
  },
  methods: {
    reset () {
      this.parentId = ''
      this.expandedKeys = []
      this.checkedKeys.checked.splice(0, this.checkedKeys.checked.length)
      this.form.resetFields()
      this.loading = false
    },
    handleCheck (checkedKeys) {
      console.log(checkedKeys)
      this.checkedKeys = checkedKeys
      console.log(checkedKeys.checked[0])
    },
    handleExpand (expandedKeys) {
      console.log(expandedKeys)
      this.expandedKeys = expandedKeys
    },
    handleSubmit (e) {
      console.log(e)
      // 只能选择一个父级按钮 点击按钮获取所有的值存储在checked数组中
      let checkedArr = Object.is(this.checkedKeys.checked, undefined) ? this.checkedKeys : this.checkedKeys.checked
      console.log(checkedArr)
      if (checkedArr.length > 1) {
        this.$message.error('最多只能选择一个上级分类，请修改')
        return
      }
      this.parentId = checkedArr[0]
      this.form.validateFields((err, values) => {
        values.parentId = this.parentId
        if (!err) {
          this.loading = true
          console.log(values)
          this.$api.goods.postCategory(values).then(() => {
            this.$emit('success')
            this.$message.success('新增类型成功')
            this.$router.push({path: `./view`})
            this.reset()
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
